// +----------------------------------------------------------------------
// | Project:   Xabad  
// +----------------------------------------------------------------------
// | CreateTime: 15/11/3  下午7:18
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.sample.ui.commons;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import cn.xabad.commons.converter.RetrofitError;
import cn.xabad.commons.sample.SampleApplication;
import cn.xabad.commons.sample.component.AppComponent;
import cn.xabad.commons.sample.tools.L;
import cn.xabad.commons.ui.CommActivity;
import com.afollestad.materialdialogs.MaterialDialog;
import com.github.johnpersano.supertoasts.SuperToast;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public abstract class BaseActivity extends CommActivity implements BaseViewInferface {

    public SuperToast toast;
    public InputMethodManager inputMethodManager;
    MaterialDialog dialog;


    @Override
    public void setting() {
        toast = new SuperToast(context);
        inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        setupComponent(SampleApplication.getComponent());
        dialog = new MaterialDialog.Builder(this).cancelable(false)
                                                 .progress(true, 0)
                                                 .build();
    }


    @Override
    public void showLoadingDialog(String s) {
        if (!checkActivityState()) { return; }
        dialog.setContent(s);
        if (dialog.isCancelled()) { dialog.show(); }
    }


    @Override
    public void showLoadingDialog(int resID) {
        if (!checkActivityState()) { return; }
        showLoadingDialog(getString(resID));
    }


    @Override
    public void hideLoadingDialog() {
        if (!checkActivityState()) { return; }
        dialog.dismiss();
    }


    @Override
    public void interError(RetrofitError error) {
        if (!checkActivityState()) { return; }
        L.e(error.getMessage());
    }


    @Override
    public void onTip(int resID) {
        if (!checkActivityState()) { return; }
        onTip(getString(resID));
    }


    protected boolean checkActivityState() {
        return activity != null && !activity.isFinishing();
    }


    @Override
    public void onTip(String message) {
        if (!checkActivityState()) { return; }
        activity.runOnUiThread(() -> {
            toast.setText(message);
            toast.setDuration(1000);
            toast.show();
        });
    }


    protected abstract void setupComponent(AppComponent appComponent);
}
